﻿@using BaBaoFan.Modifier.Model
@model IEnumerable<CoinInfoDto>
@{
    ViewData["Title"] = "八宝饭数据修改器";
}
<style>
    .layui-table-link {
        cursor: pointer;
    }
</style>
<div class="layui-body">
    <!-- 内容主体区域 -->
    <div style="padding: 15px;">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">算法类型</label>
                <div class="layui-input-inline">
                    <input id="input-algorithm" type="text" name="algorithmName" class="layui-input">
                </div>
            </div>
            <button id="btn-search" class="layui-btn">查询</button>
            <button id="btn-reset" type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
        <div class="layui-btn-group btn-operation">
            @*<button class="layui-btn" data-type="getCheckData">获取选中行数据</button>*@
            <button class="layui-btn" id="btn-add" data-type="add">添加数据</button>
            <button class="layui-btn" data-type="deleteSelectAll">删除选中的</button>
            @*<button class="layui-btn" data-type="isAll">验证是否全选</button>*@
        </div>
        <table lay-filter="coinInfo-table" id="coinInfoTable" hidden="hidden">
            <thead>
                <tr>
                    <th lay-data="{type:'checkbox', fixed: 'left'}"></th>
                    <th lay-data="{field:'id', width:100}">Id</th>
                    <th lay-data="{field:'coinName', width:150}">名称</th>
                    <th lay-data="{field:'coinShort', width:150}">简写</th>
                    <th lay-data="{field:'coinAlgorithm', width:150,templet: '#algorithmTpl'}">算法类型</th>
                    <th lay-data="{field:'coinDevice', width:150,templet: '#coinDeviceTpl'}">矿机类型</th>
                    <th lay-data="{field:'coinIsExcavate', width:150}">是否获取到矿池数据</th>
                    <th lay-data="{field:'coinSort', width:150}">排序</th>
                    <th lay-data="{field:'coinTime', width:150}">上币时间</th>
                    <th lay-data="{field:'coinUnit', width:150}">单位</th>
                    <th lay-data="{field:'coinWeight', width:150}">权重</th>
                    <th lay-data="{fixed: 'right', width:178, align:'center', toolbar: '#coinInfo-bar'}">操作</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var coinInfo in Model)
                {
                    <tr>
                        <td></td>
                        <td>@coinInfo.Id</td>
                        <td>@coinInfo.CoinName</td>
                        <td>@coinInfo.CoinShort</td>
                        <td>@coinInfo.CoinAlgorithm</td>
                        <td>@coinInfo.CoinDevice</td>
                        <td>@coinInfo.CoinIsExcavate</td>
                        <td>@coinInfo.CoinSort</td>
                        <td>@coinInfo.CoinTime</td>
                        <td>@coinInfo.CoinUnit</td>
                        <td>@coinInfo.CoinWeight</td>
                    </tr>
                }
            </tbody>
        </table>
        <script type="text/html" id="algorithmTpl">
            <a class="layui-table-link algorithm" target="_blank">{{ d.coinAlgorithm }}</a>
        </script>
        <script type="text/html" id="coinDeviceTpl">
            <div>{{ deviceFormat(d.coinDevice) }}</div>
        </script>
        <script type="text/html" id="coinInfo-bar">
            @*<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>*@
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        </script>
    </div>
</div>

<script>
    function deviceFormat(device) { 
        if (device == 1) {
            return "整机型"
        } else if (device == 2) {
            return "单卡型"
        } else {
            return "其他"
        }
    }
    layui.use('table', function () {
        var table = layui.table;


        table.init('coinInfo-table', { //转化静态表格
            //height: 'full-500'
            page: true,
            limit: 20
        });
        //$("#algorithmTable").show();//渲染完成之后显示
        //监听工具条
        table.on('tool(coinInfo-table)', function (obj) {
            var row = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        type: 'POST',
                        url: '/CoinInfo/Delete',
                        data: { ids: row.id },
                        success: function (data) {
                            console.log(data);
                            if (data == "ok") {
                                obj.del();
                                layer.close(index);
                            }
                        },
                        //dataType: 'JSON'
                    });
                });
            } else if (obj.event === 'edit') {
                window.location.href = "/CoinInfo/Edit?id=" + row.id;
            }
        });

        var $ = layui.$, active = {
            deleteSelectAll: function () { //批量删除
                var checkStatus = table.checkStatus('coinInfoTable');
                data = checkStatus.data;
                if (data.length <= 0)
                    return;
                var list = [];

                for (var i = 0; i < data.length; i++) {
                    list[i] = data[i].id;
                }
                layer.confirm('确定删除这' + data.length + '列吗?', function () {
                    $.ajax({
                        type: 'POST',
                        url: '/CoinInfo/Delete',
                        data: { ids: list },
                        success: function (data) {
                            if (data == "ok") {
                                window.location.href = "/CoinInfo/List";
                            }
                        },
                        //dataType: 'JSON'
                    });
                });
            },
            add: function () {
                window.location.href = "/CoinInfo/Add";
            }
        };

        $('.btn-operation .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        $(document).on("click", ".algorithm", function (e) {
            $('#input-algorithm').val($(this).text());
            $('#btn-search').click();
        });

        $('#btn-search').on('click', function () {
            var input_algorithm = $('#input-algorithm').val();
            //if (input_algorithm == "" && input_millName == "")
            //    return;
            table.reload("coinInfoTable", {
                url: '/CoinInfo/Search',
                method: 'POST', //方式
                where: {
                    algorithm: input_algorithm
                },
                id: 'algorithmTable',
                loading: true,
                page: true,
                limit: 20
            });
        });

        $('#btn-reset').on('click', function () {
            $('#input-algorithm').val('');
        });
    });
</script>
